﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Collections;

namespace FeatureModelCore.FMAnalyzer
{
    public class FMProductCollection
    {
        #region Properties
        public int Count { get { return _products.Count; } }
        public FMProduct[] AllProducts { get { return _products.ToArray<FMProduct>(); } }
        public FMProductItem[] AllProductItem { get { return _allItems; } }
        #endregion

        #region Fields
        /// <summary>
        /// Products
        /// </summary>
        private FMSet<FMProduct> _products;
        private FMProductItem[] _allItems;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        internal FMProductCollection(FMProductItem[] allItems)
        {
            _products = new FMSet<FMProduct>();
            _allItems = (allItems);
        }
        #endregion

        /// <summary>
        /// Return string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (_products.Count == 0)
            {
                return string.Empty;
            }
            StringBuilder strBuilder = new StringBuilder(_products[0].ToString());

            for (int i = 1; i < _products.Count; ++i)
            {
                strBuilder.AppendFormat(",{0}", _products[i]);
            }
            return strBuilder.ToString();
        }

        #region IEqualable
        /// <summary>
        /// Equals
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            FMProductCollection tmpCollection = obj as FMProductCollection;
            if(tmpCollection == null)
            {
                return false;
            }

            return this._products.Equals(tmpCollection._products);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion

        /// <summary>
        /// Add item to the end of list
        /// </summary>
        /// <param name="item"></param>
        public void Add(FMProduct item)
        {
            _products.Add(item);
        }
        /// <summary>
        /// Clear the list
        /// </summary>
        public void Clear()
        {
            _products.Clear();
        }
        /// <summary>
        /// Determin whether items in the ProductCollection
        /// </summary
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(FMProduct item)
        {
            return _products.Contains(item);
        }
        /// <summary>
        /// Copy the ProductCollection to Array
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(FMProduct[] array, int arrayIndex)
        {
            _products.CopyTo(array, arrayIndex);
        }
        /// <summary>
        /// remove the first occurence of the item in the ProductCollection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(FMProduct item)
        {
            return _products.Remove(item);
        }
    }
}
